package com.wmz.crm.workbench.controller;

import com.wmz.crm.commons.constants.Constants;
import com.wmz.crm.commons.entity.ReturnObject;
import com.wmz.crm.commons.utils.DateFormateUtil;
import com.wmz.crm.commons.utils.UUIDUtil;
import com.wmz.crm.settings.entity.DicValue;
import com.wmz.crm.settings.entity.User;
import com.wmz.crm.settings.service.DicValueService;
import com.wmz.crm.settings.service.UserService;
import com.wmz.crm.workbench.entity.Activity;
import com.wmz.crm.workbench.entity.Clue;
import com.wmz.crm.workbench.entity.ClueActivityRelation;
import com.wmz.crm.workbench.entity.ClueRemark;
import com.wmz.crm.workbench.service.ActivityService;
import com.wmz.crm.workbench.service.ClueActivityRelationService;
import com.wmz.crm.workbench.service.ClueRemrkService;
import com.wmz.crm.workbench.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class ClueController {
    @Autowired
    private UserService userService;
    @Autowired
    private DicValueService dicValueService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ClueRemrkService clueRemrkService;
    @Autowired
    private ClueService clueService;
    @Autowired
    private ClueActivityRelationService clueActivityRelationService;

    @RequestMapping("/workbench/clue/index.do")
    public String index(HttpServletRequest request){
        List<User> userList = userService.queryAllUsers();
        List<DicValue> applicationList = dicValueService.queryDicValueByTypeCode("appellation");
        List<DicValue> clueStateList = dicValueService.queryDicValueByTypeCode("clueState");
        List<DicValue> sourceList = dicValueService.queryDicValueByTypeCode("source");

        //把数据保存到request域中
        request.setAttribute("userList",userList);
        request.setAttribute("applicationList",applicationList);
        request.setAttribute("clueStateList",clueStateList);
        request.setAttribute("sourceList",sourceList);
        //请求转发到/workbench/clue/index.jsp
        return "workbench/clue/index";
    }

    @RequestMapping("/workbench/clue/saveCreateClue.do")
    @ResponseBody
    public Object saveCreateClue(Clue clue,HttpSession session){
        User user = (User) session.getAttribute(Constants.SESSION_USER);
        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        //封装参数
        clue.setId(UUIDUtil.getUUid());
        clue.setCreateTime(DateFormateUtil.formateDateTime(new Date()));
        clue.setCreateBy(user.getId());
        try {
            //调用service层方法，保存新建的线索
            int ret = clueService.saveCreateClue(clue);
            if(ret < 1) {
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统忙，请稍后重试……");
            }
        }catch (Exception e) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统忙，请稍后重试……");
            e.printStackTrace();
        }
        return returnObject;
    }

    @RequestMapping("/workbench/clue/queryClueByConditionForPage.do")
    @ResponseBody
    public Object queryClueByConditionForPage(String fullname,String company,String phone,
                                              String source,String owner,String mphone,
                                              String state,int pageNo,int pageSize) {

        //封装参数
        Map<String,Object> map = new HashMap<>();
        map.put("fullname",fullname);
        map.put("company",company);
        map.put("phone",phone);
        map.put("source",source);
        map.put("owner",owner);
        map.put("mphone",mphone);
        map.put("state",state);
        map.put("beginNo",(pageNo - 1) * pageSize);
        map.put("pageSize",pageSize);
        //调用service层方法，查询数据
        List<Clue> clueList = clueService.queryClueByConditionForPage(map);
        int totalRows = clueService.queryCountOfClueByCondition(map);
        //根据查询结果，封装响应信息
        Map<String,Object> retMap = new HashMap<>();
        retMap.put("clueList",clueList);
        retMap.put("totalRows",totalRows);
        return retMap;
    }
    @RequestMapping("/workbench/clue/queryClueById.do")
    @ResponseBody
    public Object queryClueById(String id){
        //调用service层方法，查询
        Clue clue = clueService.queryClueById(id);
        return clue;
    }

    @RequestMapping("/workbench/clue/saveEditClueById.do")
    @ResponseBody
    public Object saveEditClueById(Clue clue,HttpSession session){
        User user = (User) session.getAttribute(Constants.SESSION_USER);
        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        //封装参数
        clue.setEditBy(user.getId());
        clue.setEditTime(DateFormateUtil.formateDateTime(new Date()));
        //调用service方法，保存更新的线索信息
        try{
            int ret = clueService.saveEditClueById(clue);
            if(ret<1) {
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统忙，请稍后重试…");
            }
        }catch (Exception e) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统忙，请稍后重试…");
            e.printStackTrace();
        }
        return returnObject;
    }

    @RequestMapping("/workbench/clue/deleteClueByIds.do")
    @ResponseBody
    public Object deleteClueByIds(String[] id){
        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        try {
            //调用service层方法，删除ids线索
            int ret = clueService.deleteClueByIds(id);
            if(ret < 1) {
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统忙，请稍后重试……");
            }
        }catch (Exception e) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统忙，请稍后重试……");
            e.printStackTrace();
        }
        return returnObject;
    }

    @RequestMapping("/workbench/clue/detailClue.do")
    public String detailClue(String id,HttpServletRequest request){
        //调用service方法
        Clue clue = clueService.queryClueForDetailById(id);
        List<ClueRemark> remarkList = clueRemrkService.queryClueRemarkForDetailByClueId(id);
        List<Activity> activityList = activityService.queryActivityForDetailByClueId(id);
        //数据保存到request域中
        request.setAttribute("clue",clue);
        request.setAttribute("remarkList",remarkList);
        request.setAttribute("activityList",activityList);
        return "workbench/clue/detail";
    }

    @RequestMapping("/workbench/clue/queryActivityForDetailByNameClueId.do")
    @ResponseBody
    public Object queryActivityForDetailByNameClueId(String activityName,String clueId){
        //封装参数
        Map<String,Object> map = new HashMap<>();
        map.put("activityName",activityName);
        map.put("clueId",clueId);
        //调用service层方法
        List<Activity> activityList = activityService.queryActivityForDetailByNameClueId(map);
        return activityList;
    }

    @RequestMapping("/workbench/clue/saveBund.do")
    @ResponseBody
    public Object saveBund(String[] activityId,String clueId){
        ReturnObject returnObject = new ReturnObject();
        //封装参数 relationList市场线索
        List<ClueActivityRelation> relationList = new ArrayList<>();
        ClueActivityRelation car = null;
        for(String ai:activityId) {
            car = new ClueActivityRelation();
            car.setId(UUIDUtil.getUUid());
            car.setClueId(clueId);
            car.setActivityId(ai);
            relationList.add(car);
        }
        try {
            //调用service层方法，批量保存线索和市场的关联关系
            int ret = clueActivityRelationService.saveCreateClueActivityRelationByList(relationList);
            if(ret > 0) {
                List<Activity> activityList = activityService.queryActivityForDetailByIds(activityId);
                returnObject.setRetObj(activityList);
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
            }else {
                returnObject.setMessage("系统忙，请稍后重试……");
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            }
        }catch (Exception e) {
            returnObject.setMessage("系统忙，请稍后重试……");
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            e.printStackTrace();
        }
        return returnObject;
    }

    @RequestMapping("/workbench/clue/saveUnbund.do")
    @ResponseBody
    public Object saveUnbund(ClueActivityRelation relation) {
        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        try {
            //调用service层方法
            int ret = clueActivityRelationService.deleteClueActivityRelationClueActivityId(relation);
            if(ret < 1) {
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统忙，请稍后重试");
            }
        }catch (Exception e) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统忙，请稍后重试");
            e.printStackTrace();
        }
        return returnObject;
    }

    @RequestMapping("/workbench/clue/toConvert.do")
    public  String toConvert(String id,HttpServletRequest request) {
        //调用service层方法
        Clue clue = clueService.queryClueForDetailById(id);
        List<DicValue> stageList = dicValueService.queryDicValueByTypeCode("stage");
        //保存到request域中
        request.setAttribute("clue",clue);
        request.setAttribute("stageList",stageList);
        return "workbench/clue/convert";
    }

    @RequestMapping("/workbench/clue/queryActivityForConvertByNameClueId.do")
    @ResponseBody
    public Object queryActivityForConvertByNameClueId(@RequestParam Map<String,Object> map){
        //调用service层方法,查询关联的市场活动
        List<Activity> activityList = activityService.queryActivityForConvertByNameClueId(map);
        return activityList;
    }

    @RequestMapping("/workbench/clue/saveConvertClue.do")
    @ResponseBody
    public Object saveConvertClue(@RequestParam Map<String,Object> map,HttpSession session){
        ReturnObject returnObject = new ReturnObject();
        //封装参数
        map.put(Constants.SESSION_USER,session.getAttribute(Constants.SESSION_USER));
        try {
            //调用service层方法，实现业务功能
            clueService.saveConvertClue(map);
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        }catch (Exception e) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统忙，请稍后重试……");
        }
        return returnObject;
    }
}
